package com.lovo.project.service.impl;

import com.lovo.project.dao.RoleDAO;
import com.lovo.project.dao.UserDAO;
import com.lovo.project.enums.RoleCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.RoleDO;
import com.lovo.project.service.RoleService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.management.relation.Role;
import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/4 14:10
 */
public class RoleServiceImpl implements RoleService {
    @Override
    public RoleDO finDRoleById(Integer roleId) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDAO dao = sqlSession.getMapper(RoleDAO.class);
        try {
            RoleDO roleDO = dao.selectRoleById(roleId);
            if (roleDO == null){
                throw new OurException(RoleCodeEnum.ROLE_FIND_ERROR);
            }
            return roleDO;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public void deleteRoleById(Integer roleId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDAO dao = sqlSession.getMapper(RoleDAO.class);

        try {
            int i = dao.deleteRoleById(roleId);
            if (i<=0) {
                throw new OurException(RoleCodeEnum.ROLE_DELETE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
           throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public void addRole(RoleDO roleDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDAO dao = sqlSession.getMapper(RoleDAO.class);

        try {
            int i = dao.insertRole(roleDO);
            if (i<=0) {
                throw new OurException(RoleCodeEnum.ROLE_ADD_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public List<RoleDO> listByInfo(String roleName) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDAO dao = sqlSession.getMapper(RoleDAO.class);
        try {
            List<RoleDO> list = dao.selectRoleByInfo(roleName);
            if (list == null) {
                throw new OurException(RoleCodeEnum.ROLE_FIND_ERROR);
            }
            return list;
        } catch (OurException e) {
           throw e;
        } finally {
            sqlSession.close();
        }


    }

    @Override
    public void updateRole(RoleDO roleDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        RoleDAO dao = sqlSession.getMapper(RoleDAO.class);
        try {
            int i = dao.updateRole(roleDO);
            if (i<=0) {
                throw new OurException(RoleCodeEnum.ROLE_UPDATE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
           sqlSession.rollback();
           throw e;
        } finally {
            sqlSession.close();
        }
    }
}
